# author = suemi
# created at 16/5/13
# coding =UTF-8
from django.shortcuts import render
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.core.paginator import Paginator,InvalidPage,EmptyPage,PageNotAnInteger
from utils.QueryBuilder import QueryBuilder


def getArticle(q):
    #result = QueryBuilder.instance().term(q).cluster(True).query()
    result = QueryBuilder.instance().term(q).query()
    acticles = result['documents'][0:1000]
    returnArticles = []
    for acticle in acticles :
        rarticle = {} 
        map = acticle['highlight']
        if map.has_key('content') :
            contentArray = map['content']
            content = ""
            for c in contentArray:
                content = content + c
            rarticle['content'] = content
        else:
            rarticle['content'] =  acticle['content'][0:120]
        if map.has_key('title') :
            titleArray = map['title']
            title = ""
            for t in titleArray:
                title = title + t
            rarticle['title'] = title
        else:
            rarticle['title'] =  acticle['title']
        rarticle['allContent'] = acticle['content']
        rarticle[ u'url'] = acticle[ u'url']
        rarticle['author'] = acticle['author']
        rarticle['publishAt'] = acticle['publishAt']
        if acticle.has_key('label'):
            rarticle['label'] = acticle['label']
        else :
            rarticle['label'] = u"\u5b9e\u65f6\u65b0\u95fb"
        returnArticles.append(rarticle)
    return returnArticles

def recommandQuery(q):
    query = [u"\u6324\u7206", u"\u95e8\u7968", u"\u5929\u4ef7\u5348\u9910"]
    return query
    
def home(request):
    return render(request, 'home.html')

def search(request):
    q = request.GET['searchContent']
    if 'searchContent' in request.GET or q.isEmpty():
        acticlesAll = getArticle(q)
        recommandQ = recommandQuery(q)
        page_size = 6
        after_range_num = 5
        before_range_num = 6   
        try:
            page = int(request.GET.get("page",1))
            if page < 1:
                page = 1
        except ValueError:
            page = 1  
        paginator = Paginator(acticlesAll,page_size)
        try:
            acticles = paginator.page(page)
        except(EmptyPage,InvalidPage,PageNotAnInteger):
            acticles = paginator.page(1)
        page_range = [];
        for i in range (1,acticles.paginator.num_pages+1) :
            page_range.append(i)
        print page_range
        if page >= after_range_num:
            page_range = page_range[page-after_range_num:page+before_range_num]
        else:
            #page_range = paginator.page_range
            page_range = page_range[0:int(page)+before_range_num]
        return render_to_response('search.html', {'page_objects' : acticles, 'query':q, "page_range" : page_range, "recommandQ": recommandQ})
    else:
        message = 'You submitted an empty form.'
        return HttpResponse(message)